package edu.example;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;


public class CreateReviewFrame extends JFrame
{	
	//Variables that track the number of applications and reviewers
	private int totalNumApp = 3;
	private int totalNumReviewer;
	
	//GUI component
	private JLabel totalNumAppLabel;
	private JLabel totalNumReviewerLabel;
	private JLabel countryLabel;
	private JLabel areaLabel;
	
	
	private AssignTableModel model;
	private JTable mainTable;
	private JScrollPane scrollPane;
	
	private JPanel titlePanel;
	private JPanel filtPanel;
	private JPanel northPanel;
	private JPanel buttonPanel;
	
	private JComboBox countryComboBox;
	private JComboBox areaComboBox;
	
	private JButton assignButton;
	private JButton goButton;
	
	private GoHandler goHandler;
	private AssignHandler assignHandler;
	private CellValueHandler cellValueHandler;
	
	private UserManager um;
	private MainScreenFrame msf;
	private Vector<Reviewer> reviewerList;
	private Vector<Object[]> data;
	private Vector<Reviewer> selected;
	private Vector<String> countries = new Vector<String>();
	private Vector<String> researchArea = new Vector<String>();
	
	
	/**
	 * Constructor
	 */
	public CreateReviewFrame(UserManager initUm, MainScreenFrame initMsf)
	{
		um = initUm;
		msf = initMsf;
		intiData();
		layoutGUI();
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
	}
	
	public void intiData()
	{
		data = new Vector<Object[]>();
		selected = new Vector<Reviewer>();
		reviewerList = um.getReviewerList();
		for (int i=0; i<reviewerList.size(); i++){
			Object[] reviewerInfo = new Object[6];
			Reviewer r = reviewerList.get(i);
			reviewerInfo[0] = new Boolean(false);
			reviewerInfo[1] = r.getName();
			reviewerInfo[2] = r.getNumAssignedReviews();
			String area = r.getResearchArea1()+"/ ";
			area += r.getResearchArea2()+"/ ";
			area += r.getResearchArea3();
			reviewerInfo[3] = area;
			reviewerInfo[4] = r.getWorkload();
			reviewerInfo[5] = new Boolean(r.getChairCheck());
			data.add(reviewerInfo);
		}
		totalNumReviewer = reviewerList.size();
		
		try
		{
			FileInputStream fstream = new FileInputStream("CountryList.txt");
			  // Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			  //Read File Line By Line
			countries.add("");
			while ((strLine = br.readLine()) != null)   
			{
				countries.add(strLine);
			}
				in.close();
		}
		catch (Exception e)
		{
			System.err.println("Error: " + e.getMessage());
		}
		
		researchArea.add("");
		researchArea.add("Artificial Intelligence");
		researchArea.add("Graphics and Visualization");
		researchArea.add("Networks");
		researchArea.add("Programming languages/Verification");
		researchArea.add("Security");
		researchArea.add("Systems");
		researchArea.add("Theory");
		researchArea.add("Vision/Natural Language Processing");
	}
	
	public void layoutGUI()
	{
		//Construct GUI components
		this.setTitle("Create Assignment");
		totalNumAppLabel = new JLabel("Total Application: "+totalNumApp);
		totalNumReviewerLabel = new JLabel("Total Reviewer: "+ totalNumReviewer);
		countryLabel = new JLabel("Country: ");
		areaLabel = new JLabel("Research Area: ");
		
		goButton = new JButton("GO");
		assignButton = new JButton("Assign");
		countryComboBox = new JComboBox(countries);
		countryComboBox.setBackground(Color.WHITE);
		areaComboBox = new JComboBox(researchArea);
		areaComboBox.setBackground(Color.WHITE);
		
		filtPanel = new JPanel();
		filtPanel.setBackground(Color.WHITE);
		filtPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		filtPanel.add(countryLabel);
		filtPanel.add(countryComboBox);
		filtPanel.add(areaLabel);
		filtPanel.add(areaComboBox);
		filtPanel.add(goButton);
		
		titlePanel = new JPanel();
		titlePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		titlePanel.add(totalNumAppLabel);
		titlePanel.add(totalNumReviewerLabel);
		titlePanel.setBackground(Color.WHITE);
		
		northPanel = new JPanel();
		northPanel.setLayout(new GridLayout(2,1));
		northPanel.add(titlePanel);
		northPanel.add(filtPanel);
		
		//Setup layout
		model = new AssignTableModel(data);
		mainTable = new JTable(model);
		scrollPane = new JScrollPane(mainTable);
		mainTable.getTableHeader().setBackground(Color.WHITE);
		scrollPane.getViewport().setBackground(Color.WHITE);
		
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(assignButton);
		buttonPanel.setBackground(Color.WHITE);
		
		
		
		//Construct handler
		assignHandler = new AssignHandler();
		cellValueHandler = new CellValueHandler();
		goHandler = new GoHandler();
		
		//Link component to hander
		goButton.addActionListener(goHandler);
		assignButton.addActionListener(assignHandler);
		mainTable.addMouseListener(cellValueHandler);
		
		this.add(northPanel, BorderLayout.NORTH);
		this.add(scrollPane, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
	}
	
	class GoHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			String country = (String) countryComboBox.getSelectedItem();
			String area = (String) areaComboBox.getSelectedItem();
			Vector<Reviewer> afterFilting = new Vector<Reviewer>();
			for (int i=0; i<reviewerList.size(); i++){
				Reviewer r = reviewerList.get(i);
				Vector<String> vs = new Vector<String>();
				vs.add(r.getHomeCountry1());
				vs.add(r.getHomeCountry2());
				
				Vector<String> vs2 = new Vector<String>();
				vs2.add(r.getResearchArea1());
				vs2.add(r.getResearchArea2());
				vs2.add(r.getResearchArea3());
				
				if (!countryComboBox.getSelectedItem().equals("") && !areaComboBox.getSelectedItem().equals("")){
					if (vs.contains((String)countryComboBox.getSelectedItem()) && vs2.contains((String)areaComboBox.getSelectedItem()))
						afterFilting.add(r);
				}
				else if (!countryComboBox.getSelectedItem().equals("") && areaComboBox.getSelectedItem().equals("")){
					if (vs.contains((String)countryComboBox.getSelectedItem()))
						afterFilting.add(r);
				}
				else if (countryComboBox.getSelectedItem().equals("") && !areaComboBox.getSelectedItem().equals("")){					
					if (vs2.contains((String)areaComboBox.getSelectedItem()))
						afterFilting.add(r);
				}
				else afterFilting.add(r);
			}
			
			data = new Vector<Object[]>();
			for (int i=0; i<afterFilting.size(); i++){
				Reviewer r2 = afterFilting.get(i);
				Object[] reviewerInfo = new Object[6];
				reviewerInfo[0] = new Boolean(false);
				reviewerInfo[1] = r2.getName();
				reviewerInfo[2] = r2.getNumAssignedReviews();
				String areaN = r2.getResearchArea1()+"/ ";
				areaN += r2.getResearchArea2()+"/ ";
				areaN += r2.getResearchArea3();
				reviewerInfo[3] = areaN;
				reviewerInfo[4] = r2.getWorkload();
				reviewerInfo[5] = new Boolean(r2.getChairCheck());
				//System.out.println("table item: "+reviewerInfo[0]+reviewerInfo[1]+reviewerInfo[2]+reviewerInfo[3]+reviewerInfo[4]+reviewerInfo[5]);
				data.add(reviewerInfo);
			}
			reviewerList = afterFilting;
			totalNumReviewer = reviewerList.size();
			totalNumReviewerLabel.setText("Total Reviewer: "+ totalNumReviewer);
			totalNumReviewerLabel.repaint();
			
			model = new AssignTableModel(data);
			mainTable.setModel(model);
		}
	}
	
	class AssignHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			
			um.assignApplication(selected, totalNumApp);
			msf.updateIncompleteNum();
			//JOptionPane.showMessageDialog(CreateReviewFrame.this, "This function is not avaliable in Alpha version", "Coming Soon",JOptionPane.INFORMATION_MESSAGE);
			CreateReviewFrame.this.setVisible(false);
		}
	}
	
	class CellValueHandler implements MouseListener
	{
		public void mouseClicked(MouseEvent e) 
		{
			if (e.getButton() == e.BUTTON1){
				if (e.getClickCount() == 2){
					int row = mainTable.getSelectedRow();
					int column = mainTable.getSelectedRowCount();
					Reviewer selectedReviewer = reviewerList.get(row);
					
					editDialogInCreateAssignments temp = new editDialogInCreateAssignments(CreateReviewFrame.this, 
							selectedReviewer, row, (boolean)model.getValueAt(row, 0));
					temp.setVisible(true);
					
					System.out.println("change to "+model.getValueAt(row, column));
					reviewerList.set(row, selectedReviewer);
				}
			}
		}

		public void mouseEntered(MouseEvent arg0) {}
		public void mouseExited(MouseEvent arg0) {}
		public void mouseReleased(MouseEvent arg0) {}
		public void mousePressed(MouseEvent arg0) {}
	}
	
	public void update(Reviewer r, int index){
		reviewerList.set(index, r);
		um.updateUser(r);
		model.setValueAt(r.getAvailable(), index, 5);
		model.setValueAt(r.getWorkload(), index, 4);
		model.fireTableDataChanged();
	}
	
	public void setSelected(Reviewer r, int index, boolean select){
		selected.add(r);
		model.setValueAt(select, index, 0);
		model.fireTableDataChanged();
	}
	
	/*
	public static void main(String[] args)
	{
		UserManager um = new UserManager();
		CreateReviewFrame frame = new CreateReviewFrame(um);
		frame.setVisible(true);
	}
	*/
	
	
}
